package com.xxwy.lintcodes;

import com.xxwy.Twopoints;

/**
 * @author xxwy
 * 旋转字符串 Rotate String
 * 样例  1:
 * 输入:  str="abcdefg", offset = 3
 * 输出: "efgabcd"
 * <p>
 * 样例解释:
 * 返回旋转后的字符串。
 * <p>
 * 样例 2:
 * 输入: str="abcdefg", offset = 0
 * 输出: "abcdefg"
 * <p>
 * 样例解释:
 * 返回旋转后的字符串
 * <p>
 * 样例 3:
 * 输入: str="abcdefg", offset = 1
 * 输出: "gabcdef"
 * <p>
 * 样例解释:
 * 返回旋转后的字符串
 * <p>
 * 样例 4:
 * 输入: str="abcdefg", offset =2
 * 输出:"fgabcde"
 * <p>
 * 样例解释:
 * 返回旋转后的字符串
 */
@Twopoints
public class Num8 {
    /**
     * @param str:    An array of char
     * @param offset: An integer
     * @return: nothing
     */
    public void rotateString(char[] str, int offset) {
        // write your code here 三步翻转法
        if (str == null || str.length == 0) {
            return;
        }
        int r = str.length - 1;
        if(r-offset <=0){
            return;
        }
        int l = 0;
        rotate(str, l, r - offset);
        rotate(str, r - offset + 1, r);
        rotate(str, l, r);

    }

    private void rotate(char[] str, int l, int r) {
        while (l < r) {
            char temp = str[l];
            str[l] = str[r];
            str[r] = temp;
            l++;
            r--;
        }
    }
}
